#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <algorithm>
using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        int numa = 0, numb = 0;
        ListNode* cura = headA;
        ListNode* curb = headB;
        while (cura)
        {
            numa++;
            cura = cura->next;
        }
        while (curb)
        {
            numb++;
            curb = curb->next;
        }
        cura = headA, curb = headB;
        if (numa < numb)
        {
            swap(cura, curb);
        }
        for (int i = 0; i < abs(numa-numb); i++)
        {
            cura = cura->next;
        }
        while (cura && curb)
        {
            if (cura == curb)
                return cura;
            cura = cura->next;
            curb = curb->next;
        }
        return nullptr;
    }
};